package com.example.demo.service.impl;

import com.example.demo.mapper.OrganizationMapper;
import com.example.demo.model.Organization;
import com.example.demo.service.OrganizationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.HashMap;

/**
 * 组织架构服务实现类
 */
@Service
public class OrganizationServiceImpl implements OrganizationService {

    @Autowired
    private OrganizationMapper organizationMapper;

    @Override
    public Organization findById(Integer orgId) {
        return organizationMapper.findById(orgId);
    }

    @Override
    public List<Organization> findByParentId(Integer parentId) {
        return organizationMapper.findByParentId(parentId);
    }

    @Override
    public List<Organization> findByType(String orgType) {
        return organizationMapper.findByType(orgType);
    }

    @Override
    public List<Organization> findByParentIdAndType(Integer parentId, String orgType) {
        return organizationMapper.findByParentIdAndType(parentId, orgType);
    }

    @Override
    public Organization create(Organization organization) {
        organizationMapper.insert(organization);
        return organization;
    }

    @Override
    public Organization update(Organization organization) {
        organizationMapper.update(organization);
        return organization;
    }

    @Override
    public boolean deleteById(Integer orgId) {
        return organizationMapper.deleteById(orgId) > 0;
    }

    @Override
    public List<Organization> findAll() {
        return organizationMapper.findAll();
    }

    @Override
    public Map<String, Organization> getOrganizationHierarchy(Integer orgId) {
        Map<String, Organization> hierarchy = new HashMap<>();
        
        // 获取当前组织
        Organization currentOrg = findById(orgId);
        if (currentOrg == null) {
            return hierarchy;
        }

        // 根据组织类型设置当前组织并获取上级组织
        switch (currentOrg.getOrgType()) {
            case "CLASS":
                hierarchy.put("class", currentOrg);
                Organization major = findById(currentOrg.getParentId());
                if (major != null) {
                    hierarchy.put("major", major);
                    Organization department = findById(major.getParentId());
                    if (department != null) {
                        hierarchy.put("department", department);
                        Organization school = findById(department.getParentId());
                        if (school != null) {
                            hierarchy.put("school", school);
                        }
                    }
                }
                break;
                
            case "MAJOR":
                hierarchy.put("major", currentOrg);
                Organization department = findById(currentOrg.getParentId());
                if (department != null) {
                    hierarchy.put("department", department);
                    Organization school = findById(department.getParentId());
                    if (school != null) {
                        hierarchy.put("school", school);
                    }
                }
                break;
                
            case "DEPARTMENT":
                hierarchy.put("department", currentOrg);
                Organization school = findById(currentOrg.getParentId());
                if (school != null) {
                    hierarchy.put("school", school);
                }
                break;
                
            case "SCHOOL":
                hierarchy.put("school", currentOrg);
                break;
        }
        
        return hierarchy;
    }
    
    @Override
    public List<Organization> getOrganizationStructure(Integer orgId) {
        // 获取当前组织
        Organization currentOrg = findById(orgId);
        if (currentOrg == null) {
            return new java.util.ArrayList<>();
        }
        
        // 递归构建组织树
        List<Organization> structure = buildOrganizationTree(currentOrg);
        return structure;
    }
    
    /**
     * 递归构建组织树
     */
    private List<Organization> buildOrganizationTree(Organization root) {
        List<Organization> result = new java.util.ArrayList<>();
        
        // 构建当前节点的副本，避免修改原始数据
        Organization rootCopy = new Organization();
        rootCopy.setOrgId(root.getOrgId());
        rootCopy.setName(root.getName());
        rootCopy.setOrgType(root.getOrgType());
        rootCopy.setParentId(root.getParentId());
        rootCopy.setChildren(new java.util.ArrayList<>());
        
        // 查找所有子组织
        List<Organization> children = findByParentId(root.getOrgId());
        
        // 如果有子组织，递归构建子树
        if (children != null && !children.isEmpty()) {
            for (Organization child : children) {
                List<Organization> childTree = buildOrganizationTree(child);
                rootCopy.getChildren().addAll(childTree);
            }
        }
        
        result.add(rootCopy);
        return result;
    }
} 